// 位置选择工具
class LocationHelper {
  // 检查位置权限
  static checkLocationAuth() {
    return new Promise((resolve) => {
      wx.getSetting({
        success: (res) => {
          const authSetting = res.authSetting;
          if (authSetting["scope.userLocation"] === true) {
            resolve("authorized");
          } else if (authSetting["scope.userLocation"] === false) {
            resolve("denied");
          } else {
            resolve("notSet");
          }
        },
        fail: () => {
          resolve("error");
        },
      });
    });
  }

  // 请求位置权限
  static requestLocationAuth() {
    return new Promise((resolve) => {
      wx.authorize({
        scope: "scope.userLocation",
        success: () => {
          resolve(true);
        },
        fail: () => {
          resolve(false);
        },
      });
    });
  }

  // 选择位置
  static async chooseLocation() {
    try {
      // 检查权限状态
      const authStatus = await this.checkLocationAuth();

      if (authStatus === "denied") {
        // 用户拒绝了权限，引导去设置
        const confirmed = await this.showAuthModal();
        if (confirmed) {
          const settingRes = await this.openSetting();
          if (settingRes.authSetting["scope.userLocation"]) {
            return await this.openLocationPicker();
          }
        }
        return null;
      } else if (authStatus === "notSet") {
        // 首次请求权限
        const granted = await this.requestLocationAuth();
        if (granted) {
          return await this.openLocationPicker();
        }
        return null;
      } else if (authStatus === "authorized") {
        // 已有权限，直接选择位置
        return await this.openLocationPicker();
      }

      return null;
    } catch (error) {
      console.error("位置选择失败:", error);
      wx.showToast({
        title: "位置选择失败",
        icon: "none",
      });
      return null;
    }
  }

  // 打开位置选择器
  static openLocationPicker() {
    return new Promise((resolve) => {
      wx.chooseLocation({
        success: (res) => {
          resolve({
            name: res.name,
            address: res.address,
            latitude: res.latitude,
            longitude: res.longitude,
          });
        },
        fail: (err) => {
          console.error("位置选择器打开失败:", err);
          if (err.errMsg.includes("auth deny")) {
            wx.showToast({
              title: "需要位置权限",
              icon: "none",
            });
          } else {
            wx.showToast({
              title: "位置选择失败",
              icon: "none",
            });
          }
          resolve(null);
        },
      });
    });
  }

  // 显示权限说明弹窗
  static showAuthModal() {
    return new Promise((resolve) => {
      wx.showModal({
        title: "需要位置权限",
        content: "为了更好的体验，需要获取您的位置信息",
        confirmText: "去设置",
        cancelText: "取消",
        success: (res) => {
          resolve(res.confirm);
        },
      });
    });
  }

  // 打开设置页面
  static openSetting() {
    return new Promise((resolve) => {
      wx.openSetting({
        success: resolve,
        fail: () => {
          resolve({ authSetting: {} });
        },
      });
    });
  }

  // 获取当前位置
  static getCurrentLocation() {
    return new Promise((resolve) => {
      wx.getLocation({
        type: "gcj02",
        success: (res) => {
          resolve({
            latitude: res.latitude,
            longitude: res.longitude,
          });
        },
        fail: () => {
          resolve(null);
        },
      });
    });
  }
}

export default LocationHelper;
